home *** CD-ROM | disk | FTP | other *** search
/ NeXT Education Software Sampler 1992 Fall / NeXT Education Software Sampler 1992 Fall.iso / Programming / Source / HippoDraw / hippo / hippontuple.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-04-28  |  8.3 KB  |  407 lines

  1. /*
  2.  * hippo.c - histogramming package in C. 
  3.  *
  4.  * Copyright (C)  1991  The Board of Trustees of The Leland Stanford
  5.  * Junior University.  All Rights Reserved.
  6.  *
  7.  * $Id: hippontuple.c,v 3.9 1992/04/10 22:48:01 rensing Rel $
  8.  *
  9.  * by jonas karlsson, at SLAC, August 1990
  10.  *  split up by Paul Rensing, Feb 28,1991
  11.  */
  12.  
  13. #include <stdio.h>
  14. #include <stdlib.h>
  15. #include <math.h>
  16. /* on SGI FLT_MIN, FLT_MAX are in limits.h also */
  17. #ifndef sgi
  18. #include <float.h>
  19. #endif
  20. #include <limits.h>
  21. #include <string.h>
  22. #include "hippo.h"
  23.  
  24. GLOB_QUAL const char hippontuple_c_rcsid[] = 
  25.      "$Id: hippontuple.c,v 3.9 1992/04/10 22:48:01 rensing Rel $";
  26. GLOB_QUAL const char hippo_h_rcsid[] = HIPPO_H_RCSID;
  27.  
  28. #define BLOCKSIZE 4096        /* blocksize for allocating memory for */
  29.                 /*  data; units = number of points */
  30. #define NEW_NT_NBLOCKS 1    /* new of blocks allocated for a new ntuple */
  31.  
  32. #define INDEX(rows, columns, totcols) ((rows) * (totcols) + (columns))
  33. #define MIN(x, y)  (((x) > (y)) ? (y) : (x))
  34.  
  35. ntuple h_new(int ndim)
  36. {
  37.      ntuple nt;
  38.      int i;
  39.      char string[21];
  40.      
  41.      if ( (nt = (ntuple) malloc(sizeof(ntuple_t))) == NULL)
  42.       return NULL;
  43.      
  44.      nt->ndim = ndim;
  45.      nt->ndata = 0;
  46.      nt->rev = 0;
  47.      nt->title = NULL;
  48.      nt->label = NULL;
  49.      nt->nhigh = nt->nlow = NULL;
  50.      nt->extremeBad = 0;
  51.      
  52.      /*
  53.       * allocate memory for data.
  54.       */
  55.      if ((nt->data = 
  56.       (float *)malloc(NEW_NT_NBLOCKS*BLOCKSIZE*nt->ndim*sizeof(float)))
  57.                  == NULL)
  58.       goto error_ret;
  59.      nt->memAlloc = NEW_NT_NBLOCKS * BLOCKSIZE;
  60.  
  61.      /*
  62.       * arrays for minimum/maximum value of data dimension n.
  63.       */
  64.      if ((nt->nlow = (float *) malloc(ndim * sizeof(float))) == NULL)
  65.       goto error_ret;
  66.      if ((nt->nhigh = (float *) malloc(ndim * sizeof(float))) == NULL) 
  67.       goto error_ret;
  68.      for (i = 0; i < ndim; i++) {
  69.       nt->nlow[i] = FLT_MAX;
  70.       nt->nhigh[i] = -FLT_MAX;
  71.      }
  72.  
  73.      /*
  74.       * title of ntuple and labels for each dimension.
  75.       */
  76.      if ((nt->label = (char **)malloc(ndim * sizeof(char *))) == NULL)
  77.       goto error_ret;
  78.  
  79.      /*
  80.       * set title to blank string; XDR cannot handle NULL pointers as strings.
  81.       */
  82.      if ((nt->title = (char *)malloc( 2*sizeof(char) )) == NULL)
  83.       goto error_ret;
  84.      strcpy(nt->title, "");
  85.      for (i=0; i<ndim; i++ ) 
  86.      {
  87.       if ((nt->label[i] = (char *)malloc( 20*sizeof(char) )) == NULL)
  88.            goto error_ret;
  89.       sprintf( string, "Item %d", i);
  90.       strncpy( nt->label[i], string, 20 );
  91.      }
  92.           
  93.      return nt;
  94.  
  95. error_ret:
  96.      h_freeNt( nt );
  97.      return (ntuple) NULL;
  98. }
  99.  
  100. int h_freeNt( ntuple nt )
  101. {
  102.      int i;
  103.      
  104.      if (nt == NULL) return -1;
  105.      
  106.      /*
  107.       * free memory for data.
  108.       */
  109.      if (nt->data != NULL) free( nt->data );
  110.  
  111.      /*
  112.       * arrays for minimum/maximum value of data dimension n.
  113.       */
  114.      if (nt->nlow != NULL) free( nt->nlow );
  115.      if (nt->nhigh != NULL) free( nt->nhigh );
  116.  
  117.      /*
  118.       * title of ntuple and labels for each dimension.
  119.       */
  120.      if (nt->title != NULL) free( nt->title );
  121.      if (nt->label != NULL)
  122.      {
  123.       for (i=0; i<nt->ndim; i++ ) 
  124.            if (nt->label[i] != NULL) free( nt->label[i] );
  125.       free( nt->label );
  126.      }
  127.      
  128.      
  129.      free( nt );
  130.      
  131.      return 0;
  132. }
  133.  
  134. int h_ntSize( ntuple nt )
  135. {
  136.      int i = 0;
  137.      int j;
  138.  
  139.      if (nt == NULL) return 0;
  140.      
  141.      i += 4;            /* magic number */
  142.      i += (strlen(STRUCT_VERSION)/4 + 2)*4;
  143.      i += sizeof( ntuple_t  )+8;
  144.  
  145.      i += (strlen(nt->title)/4 + 2)*4;
  146.      for (j=0; j<nt->ndim; j++)
  147.       i += (strlen(nt->label[j])/4 + 2)*4;
  148.  
  149.      i += 2 * (nt->ndim * sizeof(float) + 4); /* nlow and nhigh */
  150.      i += nt->ndata*nt->ndim * sizeof(float) + 4;
  151.      
  152.      i += 100;            /* just in case */
  153.      
  154.      return i;
  155. }
  156.  
  157. int h_fill(ntuple nt,  ...)
  158. {
  159.      va_list argPtr;
  160.      int i;
  161.      float q;
  162.      
  163.      if (nt->ndata == nt->memAlloc) {
  164.       nt->data = (float *) realloc(nt->data, 
  165.                      (nt->memAlloc + BLOCKSIZE) 
  166.                      * nt->ndim * sizeof(float));
  167.       if (nt->data == NULL) {
  168.            return -1;
  169.       }
  170.       
  171.       nt->memAlloc += BLOCKSIZE;
  172.      }
  173.      
  174.      va_start(argPtr, nt);
  175.      
  176.      for (i = 0; i < nt->ndim; i++) {
  177.       q = nt->data[INDEX(nt->ndata, i, nt->ndim)] =
  178.            va_arg(argPtr, double);
  179.       
  180.       if (q < nt->nlow[i]) {
  181.            nt->nlow[i] = q;
  182.       }
  183.       
  184.       if (q > nt->nhigh[i]) {
  185.            nt->nhigh[i] = q;
  186.       }
  187.       
  188.      }
  189.      
  190.      nt->ndata++;
  191.      
  192.      va_end(argPtr);
  193.      
  194.      return 0;
  195. }
  196.  
  197. int h_arrayFill(ntuple nt, float *data )
  198. {
  199.      int i;
  200.      float q;
  201.      
  202.      if (nt->ndata == nt->memAlloc) {
  203.       nt->data = (float *) realloc(nt->data, 
  204.                      (nt->memAlloc + BLOCKSIZE) 
  205.                      * nt->ndim * sizeof(float));
  206.       if (nt->data == NULL) {
  207.            return -1;
  208.       }
  209.       
  210.       nt->memAlloc += BLOCKSIZE;
  211.      }
  212.      
  213.      for (i = 0; i < nt->ndim; i++) {
  214.       q = nt->data[INDEX(nt->ndata, i, nt->ndim)] = data[i];
  215.       
  216.       if (q < nt->nlow[i]) {
  217.            nt->nlow[i] = q;
  218.       }
  219.       
  220.       if (q > nt->nhigh[i]) {
  221.            nt->nhigh[i] = q;
  222.       }
  223.       
  224.      }
  225.      
  226.      nt->ndata++;
  227.      
  228.      return 0;
  229. }
  230.  
  231. int h_setNtTitle( ntuple nt, const char *title )
  232. {
  233.      int l = strlen(title);
  234.      
  235.      /* 
  236.       * allocate space for title 
  237.       */
  238.      if (nt->title != NULL) free( nt->title );
  239.      if ((nt->title = (char *) malloc((l+1) * sizeof(char))) == NULL)
  240.      {
  241.       return -1;
  242.      }
  243.      
  244.      strcpy(nt->title, title);
  245.      
  246.      return 0;
  247. }
  248.  
  249. int h_setNtLabel( ntuple nt, int dim, const char *label )
  250. {
  251.      int l = strlen(label);
  252.      
  253.      /* allocate space for label */
  254.      if (nt->label[dim] != NULL) free( nt->label[dim] );
  255.      if ((nt->label[dim] = (char *) malloc( (l+1) * sizeof(char))) == NULL)
  256.      {
  257.       return -1;
  258.      }
  259.      
  260.      strcpy(nt->label[dim], label);
  261.      
  262.      return 0;
  263. }
  264.  
  265. int h_setAllNtLabels( ntuple nt, const char *label[] )
  266. {
  267.      int i,l;
  268.      
  269.      for (i=0; i<nt->ndim; i++)
  270.      {
  271.       l = strlen(label[i]);
  272.      
  273.       /* allocate space for label */
  274.       if (nt->label[i] != NULL) free( nt->label[i] );
  275.       if ((nt->label[i] = (char *) malloc( (l+1) * sizeof(char))) == NULL)
  276.       {
  277.            return -1;
  278.       }
  279.       
  280.       strcpy(nt->label[i], label[i]);
  281.      }
  282.      
  283.      return 0;
  284. }
  285.  
  286.  
  287. int h_clrNt( ntuple nt )
  288. {
  289.      int i;
  290.      
  291.      nt->ndata = 0;
  292.  
  293.      /*
  294.       * allocate memory for data.
  295.       */
  296.      if ((nt->data = 
  297.       (float *)realloc(nt->data,
  298.                NEW_NT_NBLOCKS*BLOCKSIZE*nt->ndim*sizeof(float)))
  299.                  == NULL)
  300.      {
  301.       return -1;
  302.      }
  303.      nt->memAlloc = NEW_NT_NBLOCKS * BLOCKSIZE;
  304.  
  305.      for (i = 0; i < nt->ndim; i++)
  306.      {
  307.       nt->nlow[i] = FLT_MAX;
  308.       nt->nhigh[i] = -FLT_MAX;
  309.      }
  310.      nt->rev++;
  311.      
  312.      return 0;
  313. }
  314.  
  315.  
  316. const char *h_getNtLabel( ntuple nt, int dim )
  317. {
  318.      if (nt != NULL && dim>=0 && dim < nt->ndim) return nt->label[dim];
  319.      return NULL;
  320. }
  321.  
  322. const float *h_getNtData( ntuple nt, int i_nt )
  323. {
  324.      if (nt == NULL) return NULL;
  325.      if (i_nt >= nt->ndata) return NULL;
  326.      
  327.      return &( nt->data[INDEX(i_nt,0,nt->ndim)] );
  328. }
  329.  
  330. float *h_getNtColumn( ntuple nt, int dim )
  331. {
  332.      float *col, *d, *cd;
  333.      float *last;
  334.      
  335.      if (nt == NULL) return NULL;
  336.      if (dim < 0 || dim >= nt->ndim) return NULL;
  337.      
  338.      /*
  339.       * allocate array.
  340.       */
  341.      if ( (col = (float *) malloc( nt->ndata * sizeof(float) )) == NULL)
  342.       return NULL;
  343.      
  344.      last = &( nt->data[INDEX(nt->ndata,0,nt->ndim)] );
  345.      for (d = &(nt->data[INDEX(0,dim,nt->ndim)]), cd = col;
  346.       d < last;
  347.       d += nt->ndim, cd++)
  348.      {
  349.       *cd = *d;
  350.      }
  351.           
  352.      return col;
  353. }
  354.  
  355.  
  356. int h_replData( ntuple nt, int i_nt, ... )
  357. {
  358.      va_list argPtr;
  359.      int i;
  360.      float q;
  361.      
  362.      if (i_nt >= nt->ndata) return -1;
  363.  
  364.      va_start(argPtr, i_nt);
  365.      
  366.      for (i = 0; i < nt->ndim; i++) 
  367.      {
  368.       q = nt->data[INDEX(i_nt, i, nt->ndim)] =
  369.            va_arg(argPtr, double);
  370.       
  371.       if (q < nt->nlow[i]) nt->nlow[i] = q;
  372.       if (q > nt->nhigh[i]) nt->nhigh[i] = q;
  373.      }
  374.  
  375.      va_end(argPtr);
  376.      
  377.      nt->rev++;
  378.      if (nt->rev > INT_MAX-100) nt->rev = 0;
  379.      
  380.      nt->extremeBad = 1;
  381.      
  382.      return 0;
  383. }
  384.  
  385. int h_arrayReplData( ntuple nt, int i_nt, float *data )
  386. {
  387.      int i;
  388.      float q;
  389.      
  390.      if (i_nt >= nt->ndata) return -1;
  391.  
  392.      for (i = 0; i < nt->ndim; i++) 
  393.      {
  394.       q = nt->data[INDEX(i_nt, i, nt->ndim)] = data[i];
  395.       
  396.       if (q < nt->nlow[i]) nt->nlow[i] = q;
  397.       if (q > nt->nhigh[i]) nt->nhigh[i] = q;
  398.      }
  399.      
  400.      nt->rev++;
  401.      if (nt->rev > INT_MAX-100) nt->rev = 0;
  402.  
  403.      nt->extremeBad = 1;
  404.      
  405.      return 0;
  406. }
  407.